//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Option.h"
using namespace Cephei::QL;
#include <gen/QL/Exercise.h>
#include <gen/QL/Payoff.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instrument.h>
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::COption::COption (boost::shared_ptr<QuantLib::Option>& childNative, Object^ owner) : CInstrument(COption::typeid)
{
#ifdef HANDLE
	_phOption = NULL;
#endif
	_ppOption = &childNative;
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppOption));
}
Cephei::QL::COption::COption (QuantLib::Option& childNative, Object^ owner) : CInstrument(COption::typeid)
{
#ifdef HANDLE
	_phOption = NULL;
#endif
	_ppOption = new boost::shared_ptr<QuantLib::Option> (&childNative);
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppOption));
    _OptionOwner = owner;
    _InstrumentOwner = owner;
}

Cephei::QL::COption::COption (COption^ copy) : CInstrument(COption::typeid)
{
#ifdef HANDLE
	_phOption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppOption = new boost::shared_ptr<QuantLib::Option> (copy->GetShared());
        _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppOption));
    }
}
Cephei::QL::COption::COption (System::Type^ t) : CInstrument(COption::typeid)
{
#ifdef HANDLE
	_phOption = NULL;
#endif
	if (!t->IsSubclassOf(COption::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::COption::COption (QuantLib::Handle<QuantLib::Option>& childNative, Object^ owner)  : CInstrument(COption::typeid)
{
	_phOption = &childNative;
	_ppOption = &static_cast<boost::shared_ptr<QuantLib::Option>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppOption));
    _OptionOwner = owner;
}
Cephei::QL::COption::COption (QuantLib::Handle<QuantLib::Option> childNative)  : CInstrument(COption::typeid)
{
	_phOption = &childNative;
	_ppOption = &static_cast<boost::shared_ptr<QuantLib::Option>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppOption));
}
#endif
#ifdef STRUCT
Cephei::QL::COption::COption (QuantLib::Option childNative)  : CInstrument(COption::typeid)
{
#ifdef HANDLE
	_phOption = NULL;
#endif
	_ppOption = new boost::shared_ptr<QuantLib::Option> (new QuantLib::Option (childNative));
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppOption));
}
#endif

Cephei::QL::COption::~COption ()
{
    if (_ppOption != NULL)
    {
	    delete _ppOption;
        _ppOption = NULL;
    }
}
Cephei::QL::COption::!COption ()
{
    if (_ppOption != NULL)
    {
	    delete _ppOption;
    }
}
QuantLib::Option& Cephei::QL::COption::GetReference ()
{
    if (_ppOption == NULL) throw gcnew NativeNullException ();
	return **_ppOption;
}
boost::shared_ptr<QuantLib::Option>& Cephei::QL::COption::GetShared ()
{
    if (_ppOption == NULL) throw gcnew NativeNullException ();
	return *_ppOption;
}
QuantLib::Option* Cephei::QL::COption::GetPointer ()
{
    if (_ppOption == NULL) throw gcnew NativeNullException ();
	return &**_ppOption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Option>& Cephei::QL::COption::GetHandle ()
{
	if (_phOption == NULL)
	{
		_phOption = new Handle<QuantLib::Option> (*_ppOption);
	}
	return *_phOption;
}
#endif
bool Cephei::QL::COption::HasNative () 
{
	return (_ppOption != NULL);
}

Cephei::QL::IExercise^ Cephei::QL::COption::Exercise::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	boost::shared_ptr<QuantLib::Exercise> _rv = (boost::shared_ptr<QuantLib::Exercise>)(*_ppOption)->exercise ( );   
        Cephei::QL::CExercise^ _nrv = gcnew Cephei::QL::CExercise (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IPayoff^ Cephei::QL::COption::Payoff::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	boost::shared_ptr<QuantLib::Payoff> _rv = (boost::shared_ptr<QuantLib::Payoff>)(*_ppOption)->payoff ( );   
        Cephei::QL::CPayoff^ _nrv = gcnew Cephei::QL::CPayoff (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

